package com.pw.study.realtime.handle

import com.pw.study.realtime.bean.StartBean
import com.pw.study.realtime.utils.MyUtils
import org.apache.spark.rdd.RDD
import redis.clients.jedis.Jedis

import java.util
import java.util.Properties

object RedisHandler {
  private val properties: Properties = MyUtils.load("config.properties")
  private val redisHost: String = properties.getProperty("redis.host")
  private val redisPort: Int = properties.getProperty("redis.port").toInt

  def rddToFilter(rddETL: RDD[StartBean]): RDD[StartBean] = {
   rddETL.mapPartitions(partition => {
      val jedis = RedisHandler.getJedisClient()
      //redis作为中间变量
      val beans = partition.filter(bean => !jedis.sismember("DAU" + bean.day, bean.mid))
      jedis.close()
      beans
    })
  }

  def main(args: Array[String]): Unit = {
    val redisConn: Jedis = getJedisClient()
    val key = redisConn.keys("*").toString
    val str = redisConn.set("hello", "redis")
    val set: util.Set[String] = redisConn.smembers("oo")
    set.forEach(r=>{
      println(s"hello;; ${r}")
    })
    println(set)
    println(key)
    println(str)
    val str1 = redisConn.get("hello")
    println(str1)


    println(getJedisClient())
  }

  def getJedisClient(): Jedis = {
    new Jedis(redisHost, redisPort)
  }

}
